﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using NopSolutions.NopCommerce.BusinessLogic.Data;
using NopSolutions.NopCommerce.BusinessLogic.Model;

namespace NopSolutions.NopCommerce.BusinessLogic.Services
{
    public static class AclCompiledQueries
    {
        public static readonly Func<NopObjectContext, int, int, IQueryable<int>> IsAccessDenied =
            CompiledQuery.Compile<NopObjectContext, int, int, IQueryable<int>>(
                (ctx, customerId, categoryId) =>
                    from c in ctx.Customers
                    from cr in c.NpCustomerRoles
                    from acl in ctx.ACLPerObject
                    where
                        c.CustomerId == customerId &&
                        acl.ObjectId == categoryId &&
                        acl.ObjectTypeId == (int)ObjectTypeEnum.Category &&
                        acl.Deny == true &&
                        acl.CustomerRoleId == cr.CustomerRoleId
                    select cr.CustomerRoleId
            );
    }
}
